package com.jtcoding.chat.test;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jtcoding.chat.common.FileUtils;
import com.jtcoding.chat.common.JacksonSerializer;
import com.jtcoding.chat.common.QRcodeUtils;
import com.jtcoding.chat.model.User;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.List;

/**
 * @author jason.tang
 * @create 2019-02-19 9:13
 * @description
 */
public class GenerateQRCode {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        LocalDate effectiveDate = LocalDate.now();

        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = effectiveDate.atStartOfDay(zoneId);
        Date tempEffectiveDate = Date.from(zonedDateTime.toInstant());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempEffectiveDate);
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        LocalDate dueDate = LocalDate.parse(dateFormat.format(calendar.getTime()));

        System.out.println(dueDate);
        System.out.println(dueDate.isBefore(LocalDate.now()) || dueDate.isEqual(LocalDate.now()));

        long start1 = System.currentTimeMillis();
        for (int i = 1; i < 100; i++) {
            System.out.println(i + ": " + getPrimeNumber(i));
        }
        long end1 = System.currentTimeMillis();

        System.out.println("time1: " + (end1 - start1));

        long start2 = System.currentTimeMillis();
        for (int i = 0; i <= 100; i++) {
            System.out.println(i + ": " + calc(i));
        }
        long end2 = System.currentTimeMillis();
        System.out.println("time2: " + (end2 - start2));
    }

    private static List<Integer> calc(int number) {
        List<Integer> factors = new ArrayList<>();

        while (number > 1) {
            for (int factor = 2; factor <= number; factor++) {
                if (number % factor == 0 && number > factor) {
                    factors.add(factor);
                    number = number / factor;
                    break;
                }
            }
        }
        return factors;
    }

    private static List<Integer> getPrimeNumber(int num) {
        List<Integer> result = new ArrayList<>();
        if (isPrimeNumber(num)) {
            result.add(num);
        } else {
            for (int i = 2; i <= num; i++) {
                if (isPrimeNumber(i) && num % i == 0) {
                    num = num / i;
                    result.add(i);
                    i = 1;
                    if (isPrimeNumber(num)) {
                        result.add(num);
                        break;
                    }
                }
            }
        }
        return result;
    }
    // 判断是否为质数
    private static boolean isPrimeNumber(int num) {
        if (num <= 3) {
            return true;
        }
        if (num % 6 != 1 && num % 6 != 5) {
            return false;
        }
        int tmp = (int) Math.sqrt(num);
        for (int i = 5; i <= tmp; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

//    private static boolean isPrimeNumber(int number) {
//        if (number <= 3) {
//            return true;
//        }
//        for (int i = 2; i < number; i++) {
//            if (number % i == 0) {
//                return false;
//            }
//        }
//        return true;
//    }



}